import Vue from 'vue'
import VueRouter from 'vue-router'
import { getToken } from '@/utils/storage'
// import HomeView from '../views/HomeView.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    name: 'login',
    // redirect: '/login',
    component: () => import('../views/login.vue')
  },

  {
    path: '/welcome',
    name: 'welcome',
    redirect: '/ssss',
    component: () => import('../views/welcome.vue'),
    children: [
      {
        path: '/ssss',
        name: 'ssss',
        // redirect: '/login',
        component: () => import('../views/ssss.vue')
      },
      {
        path: '/users',
        name: 'users',
        component: () => import('../views/users.vue')
      },
      {
        path: '/goods/add',
        name: 'add',
        component: () => import('../views/goodAdd.vue')
      },
      {
        path: '/roles',
        name: 'roles',
        component: () => import('../views/roles.vue')
      },
      {
        path: '/rights',
        name: 'rights',
        component: () => import('../views/rights.vue')
      },
      {
        path: '/goods',
        name: 'goods',
        component: () => import('../views/goods.vue')
      },
      {
        path: '/params',
        name: 'params',
        component: () => import('../views/params.vue')
      },
      {
        path: '/categories',
        name: 'categories',
        component: () => import('../views/categories.vue')
      },
      {
        path: '/orders',
        name: 'orders',
        component: () => import('../views/orders.vue')
      },
      {
        path: '/reports',
        name: 'reports',
        component: () => import('../views/reports.vue')
      },

    ]
  }
  // {
  //   path: '/',
  //   name: 'home',
  //   component: HomeView
  // },
  // {
  //   path: '/about',
  //   name: 'about',
  //   // route level code-splitting
  //   // this generates a separate chunk (about.[hash].js) for this route
  //   // which is lazy-loaded when the route is visited.
  //   component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
  // }
]

const router = new VueRouter({
  mode: 'hash',
  base: process.env.BASE_URL,
  routes
})
const whiteList = ['/login']
router.beforeEach((to, from, next) => {
  //判断
  const token = getToken()
  //如果有token
  if (token) {
    next()
  }
  //如果没有token
  else {
    if (whiteList.includes(to.path)) {
      next()
    }
    else {
      next('/login')
    }
  }
})
//保存原型对象的Push
let originPush = VueRouter.prototype.push
let originReplace = VueRouter.prototype.replace
//重写push方法
VueRouter.prototype.push = function (location, res, rej) {
  if (res && rej) {
    originPush.call(this, location, res, rej)
  } else {
    originPush.call(this, location, () => { }, () => { })
  }
}
//重写replace方法
VueRouter.prototype.replace = function (location, res, rej) {
  if (res && rej) {
    originReplace.call(this, location, res, rej)
  } else {
    originReplace.call(this, location, () => { }, () => { })
  }
}
export default router
